<?php

namespace App\Controller\Admin;

use App\Controller\Example\RechargelExample;
use App\Controller\Example\UserExample;
use App\Controller\Home\Funds;
use App\Models\AdminModel;
use App\Models\Conn;
use App\Models\RechargeModel;
use App\Models\UsersModel;
use Core\Common\Common;
use Core\Enums\ErrorEnum;
use Core\Enums\FundsEnum;
use Core\Enums\FundsGenreEnum;
use Core\Result\Result;

class Recharge
{
    /**
     * 管理后台充值列表
     * entry token,startTime,endTime,page,limit
     * return json
     * */
    public function rechargeList($data){
        $assemble = Common::assemble(new RechargelExample(), $data);
        $identical = Common::identical($data);
        $conn = new Conn();
        $joinSql = " WHERE `state`>0 ";
        if($assemble->uid !=null){ $joinSql.=" and `uid` ='$assemble->uid' ";}
        if($assemble->notes !=null){ $joinSql.=" and `notes` ='$assemble->notes' ";}
        if($assemble->state !=null){ $joinSql.=" and `state` ='$assemble->state' ";}
        if($assemble->serial !=null){ $joinSql.=" and `serial` ='$assemble->serial' ";}
        if($assemble->startTime !=null){ $assemble->startTime = strtotime($assemble->startTime);$joinSql.=" and UNIX_TIMESTAMP(`time`) >= '$assemble->startTime' ";}
        if($assemble->endTime !=null){ $assemble->endTime = strtotime($assemble->endTime);$joinSql.=" and UNIX_TIMESTAMP(`time`) <= '$assemble->endTime' ";}
        $userSql = "SELECT `id`,`uid`,`serial`, `proof`, `amount`, `notes`, `time`, `state` FROM `recharge` $joinSql order by id desc limit $identical->pageStart,$identical->limit";
        $readConn = $conn->readConn($userSql,true);
        $returnData["list"]=$readConn;
        $userCountSql="SELECT count(id) as count FROM `recharge` $joinSql";
        $userTotalSql="SELECT sum(`amount`) as 总金额 FROM `recharge` $joinSql limit $identical->pageStart,$identical->limit";
        $returnData["count"] = $conn->readConn($userCountSql)["count"];
        $returnData["total"] = $conn->readConn($userTotalSql);
        Result::success($returnData);
    }

    /**
     * 管理后台编辑充值单
     * entry token,amount,state
     * return json
     * */
    public function editRecharge($data){
        $assemble = Common::assemble(new RechargelExample(), $data);
        $conn = new Conn();
        $adminInToken = AdminModel::getAdminInToken($conn, $assemble->token);
        $AssetExample = Common::assemble(new RechargelExample(), RechargeModel::getRechargeInsideId($conn, $assemble->id));
        if($AssetExample->state != 1){
            Result::error(ErrorEnum::PROHIBITMODIFICATION);
        }
        $conn->shiwuStart();
        if(RechargeModel::editRechargeInside($conn,$assemble) !=1){
            $conn->shiwuRollback();
            Result::error(ErrorEnum::SUBMITFAILED);
        }
        $conn->shiwuEnd();
        AdminLog::addAdminLog($adminInToken["username"],"充值列表","编辑充值:[".$assemble->id."]金额:".$assemble->amount);
        Result::success("ok");
    }
    /**
     * 管理后台充值单通过
     * entry token
     * return json
     * */
    public function passRecharge($data){
        $assemble = Common::assemble(new RechargelExample(), $data);
        $conn = new Conn();
        $adminInToken = AdminModel::getAdminInToken($conn, $assemble->token);
        if(empty($adminInToken) || $adminInToken["level"] !=1){
            Result::error(ErrorEnum::NOPERMISSIONTOSUBMIT);
        }
        $RechargelExample = Common::assemble(new RechargelExample(), RechargeModel::getRechargeInsideId($conn, $assemble->id));
        if($RechargelExample->state != 1){
            Result::error(ErrorEnum::ILLEGALOPERATION);
        }
        $orderMoney = $RechargelExample->amount;
        if($orderMoney <= 0 ){
            Result::error(ErrorEnum::ABNORMALAMOUNT);
        }
        $UserExample = Common::assemble(new UserExample(),UsersModel::getUserInsideId($conn, $RechargelExample->uid));
        $fundsData["number"] =$RechargelExample->serial;
        $fundsData["notes"]=FundsEnum::RECHARGE;
        $fundsData["uid"] = $RechargelExample->uid;
        $fundsData["type"]=FundsEnum::RECHARGE;
        $conn->shiwuStart();
        $editUserMoney = UsersModel::editUserMoneyAddInside($conn, $orderMoney, $UserExample);
        $fundsData["genre"]=FundsGenreEnum::MONEY;
        $fundsData["amount"]=$orderMoney;
        $fundsData["balance"]=$UserExample->money + $orderMoney;
        $userMoneyWriteFunds = Funds::writeFunds($conn, $fundsData);
        $assemble->state = 2;
        $editRechargeInside = RechargeModel::editRechargeInside($conn, $assemble);
        if($editUserMoney !=1 || $userMoneyWriteFunds !=1 || $editRechargeInside !=1){
            $conn->shiwuRollback();
            Result::error(ErrorEnum::AUDITFAILED);
        }
        $conn->shiwuEnd();
        AdminLog::addAdminLog($adminInToken["username"],"充值列表","通过充值uid:[".$RechargelExample->uid."]");
        Result::success("ok");
    }
    /**
     * 管理后台充值单不通过
     * entry token,amount,state
     * return json
     * */
    public function noPassRecharge($data){
        $assemble = Common::assemble(new RechargelExample(), $data);
        $conn = new Conn();
        $adminInToken = AdminModel::getAdminInToken($conn, $assemble->token);
        if(empty($adminInToken) || $adminInToken["level"] !=1){
            Result::error(ErrorEnum::NOPERMISSIONTOSUBMIT);
        }
        $assemble->state = 3;
        $editRechargeInside = RechargeModel::editRechargeInside($conn, $assemble);
        if($editRechargeInside !=1){
            $conn->shiwuRollback();
            Result::error(ErrorEnum::AUDITFAILED);
        }
        $conn->shiwuEnd();
        AdminLog::addAdminLog($adminInToken["username"],"充值列表","不通过充值:[".$assemble->id."]");
        Result::success("ok");
    }


}